package com.fivefu.core.skyeye.event.filter;

import com.fivefu.base.common.utils.str.StrUtils;
import com.fivefu.core.module.auth.properties.AuthClientProperties;
import com.fivefu.core.module.auth.utils.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class LoginHandlerInterceptor implements HandlerInterceptor {

    @Autowired
    private AuthClientProperties authClientProperties;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (authClientProperties != null){
            if (authClientProperties.isEnableSecurity()){
                Long userid = SecurityUtil.currentUser().getUserId();
                request.setAttribute("userid", userid);
            } else {
                if (StrUtils.isNotNull(request.getParameter("userid"))){
                    request.setAttribute("userid", request.getParameter("userid"));
                }
            }
        }

        /*String token = request.getHeader("token");
        if (StringUtils.isEmpty(token)){
            ResultInfo result = ResultInfo.renderError("未登录");

            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json; charset=utf-8");

            PrintWriter writer = response.getWriter();
            writer.append(JSON.toJSONString(result));
            return false;
        }*/
        return true;
    }

}
